<!--  -->
<template>

  <div class="login">
     <div class="picd">
         <img src="../assets/jd.jpg"/>
     </div>
     <cube-form
  :model="model"
  :schema="schema"
  @submit="submitHandler"></cube-form>
  </div>
</template>

<script>

export default {
  data () {
    return {
        model:{
            username:'',
            password:''
        },
          schema: {
              fields:[
                //   登录
                     {
                       type:"input",
                       modelKey: 'username',
                       label: '用户名',
                       props: {
                       placeholder: '请输入用户名'
                        },
                        rules:{
                            // 校验规则
                            required:true,
                            type:"string",
                            min:3,
                            max:15,
                        },
                        trigger:"blur",
                        messages:{
                             required:"用户名不能为空",
                             min:"用户名不能少于3个字符",
                             max:"用户名不能大于十五个字符"
                        }
                  },
                //   密码
                   {
                       type:"input",
                       modelKey: 'password',
                       label: '密码',
                       props: {
                       placeholder: '请输密码',
                       type:"password",
                       eye:{
                           open:false
                       }
                        },
                        rules:{
                            // 校验规则
                            required:true,
                        },
                        trigger:"blur",   
                     },
                     {
                         type:"submit",
                         label:"登录"
                         }
              ]
          }
    };
  },
  methods:{
   async  submitHandler(e){
            e.preventDefault()
    //    this.$http.get('/api/login',{params:this.model}).then(res=>{
    //                      console.log(res.data.token)
    //       }).catch(err=>{
    //           console.log(err)
    //       })
       try{
             const result=await this.$http.get("/api/login",{params:this.model})
                console.log(result.token)
                if(result.code=="0"){
                     this.$store.commit("seeoken",result.token)
                     window.localStorage.setItem("token",result.token)
                    
                    //  判断路由是否带参数，带参数就去重定向参数地址，否则就去首页
                    if(this.$route.query.redirect){
              this.$router.replace({path:this.$route.query.redirect})
                    }else{
                         this.$router.replace({path:"/batnav/index"})
                    }
                }else{
                    alert(result.message)
                }
       }catch(err){
           console.log(err)
       }
      }
  }

}

</script>
<style>
.login{
    width: 100%;
    height: 100%;

}
.login .picd{
    width: 100%;
    height: 200px;
    display: flex;
    align-items:center;
  justify-content: center;
}
.login .picd img{
   width: 100%;
   height: 200px;
}


</style>